//
//  HykHomeViewController.swift
//  sinaWeiBo
//
//  Created by hyk on 15/11/23.
//  Copyright © 2015年 hyk. All rights reserved.
//

import UIKit

import AFNetworking

import SVProgressHUD

class HykHomeViewController: HykBasicViewController {

    // MARK: - 属性
    
    /// cell重用标示
    private let ReuseIdentifier = "ReuseIdentifier"
    
    /// 微博模型数组
    
    var statuses:[HykStatus]?{
        didSet{
           //更新数据
            tableView.reloadData()
        }
    
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //只有不满足登录条件，用户就不显示,执行以下操作
    
        if !userlogin {
          return
        }
        setNavigationBar()
        
        //注册一个cell
        prepareTableView()
        //获取微博数据
//        HykNetworkTool.shardInstance.loadStatus{ (result, error) -> () in
//            print("微博数据----加载失败：\(error)")
//            print("微博数据----加载成功：\(result)")
//        }
        HykStatus.loadStatus { (statuses, error) -> () in
            //判断是否错误
            if error != nil {
              print("加载数据出错error:\(error) ")
             SVProgressHUD.showErrorWithStatus("网络不给力")
             return
            }
            print("加载微博模型成功：\(statuses)")
             // 将加载好的数据赋值给微博数组属性
            self.statuses = statuses
            
        }
    }
    
    
    private func prepareTableView() {
        // 注册cell
        tableView.registerClass(HykStatusCell.self, forCellReuseIdentifier: ReuseIdentifier)
        
        // 设置行高
        tableView.rowHeight = 200
    }
     private func setNavigationBar(){
//       let leftButton = UIButton()
//        leftButton.setBackgroundImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
//        leftButton.setBackgroundImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
//        leftButton.sizeToFit()
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
//        
//        let ringhtButton = UIButton()
//        ringhtButton.setBackgroundImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
//        ringhtButton.setBackgroundImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
//        ringhtButton.sizeToFit()
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: ringhtButton)
//    }
    
      navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
       
        //设置标题
        //创建按钮
        //？？ 如果前面有值就取前面的，没有值就取后面的
        let screen_name = HykUserAccount.loadUserAccount()?.screen_name ?? "疯子"
        let homeTitleButton = HykHomeTitleButton(title: screen_name)
        
        navigationItem.titleView = homeTitleButton
        
        //添加按钮的点击事件
        homeTitleButton.addTarget(self, action: "homeTitleButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    //按钮的点击动画
    func homeTitleButtonClick(button:UIButton){
       // 点击修改按钮的选中状态
        button.selected = !button.selected
        
        // 根据按钮的选中状态来开始动画
        // 按钮选中的时候,箭头转到上面去, 按钮没有选中的时候,箭头转到下面来
        
        // UIView旋转动画: 就近原则 如果旋转 270: 360 -270 = 90, 如果2边一样远,会顺时针
        //        if button.selected {
        //            UIView.animateWithDuration(0.25, animations: { () -> Void in
        //                button.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01))
        //            })
        //        } else {
        //            UIView.animateWithDuration(0.25, animations: { () -> Void in
        //                button.imageView?.transform = CGAffineTransformIdentity
        //            })
        //        }
       
        let transfrom = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01)) : CGAffineTransformIdentity
        //开始动画
        UIView.animateWithDuration(0.25) { () -> Void in
            button.imageView?.transform = transfrom
        }
    
    }
    
    // MARK: - tableView数据源
    /// 返回多行数据
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //返回cell的数量
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! HykStatusCell
        //设置cell的数据
//        cell.textLabel?.text = statuses?[indexPath.row].text
        
        // 给cell设置模型
        cell.status = statuses?[indexPath.row]
        return cell
    }
    
    
    
}
